package top.hmtools.callBacks;

import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.input.MouseEvent;
import javafx.util.Callback;
import top.hmtools.beans.MapperXmlFileTempletBean;
import top.hmtools.enums.EDBType;

public class MapperXmlFileTempletListCallBack
        implements
            Callback<TreeView<String>, TreeCell<String>> {

    private Callback<TreeView<String>, TreeCell<String>> defaultCellFactory;
    private TextField tf_id;
    private TextField tf_templetName;
    private ChoiceBox<String> cb_dbType;
    private TextArea tf_templetContent;
    private TextArea tf_comment;

    public MapperXmlFileTempletListCallBack(
            Callback<TreeView<String>, TreeCell<String>> defaultCellFactory,
            TextField tf_id, TextField tf_templetName,
            ChoiceBox<String> cb_dbType, TextArea tf_templetContent,
            TextArea tf_comment) {
        this.defaultCellFactory = defaultCellFactory;
        this.tf_id = tf_id;
        this.tf_templetName = tf_templetName;
        this.cb_dbType = cb_dbType;
        this.tf_templetContent = tf_templetContent;
        this.tf_comment = tf_comment;
    }

    /**
     * 方法说明 输入参数说明 输出参数说明
     */
    @Override
    public TreeCell<String> call(TreeView<String> param) {
        TreeCell<String> result = defaultCellFactory.call(param);
        result.addEventHandler(MouseEvent.MOUSE_CLICKED,
                new EventHandler<Event>() {

                    @Override
                    public void handle(Event event) {
                        TreeCell<String> treeCell = (TreeCell<String>) event.getSource();
                        TreeItem<String> treeItem = treeCell.getTreeItem();
                        if(treeItem==null){
                        	return;
                        }
                        MapperXmlFileTempletBean userData = (MapperXmlFileTempletBean) treeItem.getGraphic().getUserData();

                        tf_id.setText(userData.getId());
                        tf_templetName.setText(userData.getName());
                        tf_templetContent.setText(userData.getContent());
                        tf_comment.setText(userData.getComment());

                        int db_type = userData.getDb_type();
                        EDBType edbType = EDBType.getByTypeCode(db_type);
                        cb_dbType.setValue(edbType.getName());

                    }

                });
        return result;
    }

}
